package com.wm.task.server.handler;

import com.wm.task.common.util.Command;
import com.wm.task.common.vo.Student;
import com.wm.task.server.dao.StudentDAO;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;

public class SocketHandler {
    private StudentDAO studentDAO = StudentDAO.getInstance();
    private Socket socket;
    private InputStream is;
    private OutputStream os;

    public SocketHandler(Socket socket) {
        if (socket == null) {
            throw new RuntimeException();
        }
        this.socket = socket;
    }

    private void initStream() {
        try {
            this.is = socket.getInputStream();
            this.os = socket.getOutputStream();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void startProcess() {
        new Thread(() -> {
            this.initStream();
            this.process();
        }).start();
    }

    private void handleCommand(int command) {
        switch (command) {
            case Command.LIST:
                handleList();
                break;
            case Command.ADD:
                handleAdd();
                break;
            case Command.DELETE:
                handleDelete();
                break;
        }
    }

    private void handleAdd() {
        try {
            ObjectInputStream ois = new ObjectInputStream(is);
            Student student = (Student) ois.readObject();
            studentDAO.save(student);
            ois.close();
            this.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void handleDelete() {
        try {
            ObjectInputStream ois = new ObjectInputStream(is);
            ObjectOutputStream oos = new ObjectOutputStream(os);
            Student student = (Student) ois.readObject();
            oos.writeObject(studentDAO.delete(student));
            oos.close();
            ois.close();
            this.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void handleList() {
        List<Student> students = studentDAO.findAll();
        //把这个送给客户端
        try {
            ObjectOutputStream oos = new ObjectOutputStream(os);
            oos.writeObject(students);
            oos.flush();
            oos.close();
            System.out.println("发送列表信息");
            this.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void process() {
        //读取客户端的命令字1byte
        try {
            int command = is.read();
            System.out.println("服务器接收到：" + command);
            handleCommand(command);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void close() {
        try {
            this.os.close();
            this.is.close();
            this.socket.close();
        } catch (Exception e) {
        }
    }
}
